package com.hn.gege.sys.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hn.gege.sys.modules.sys.service.IUserRoleService;
import com.hn.gege.sys.modules.sys.dto.RoleUserDTO;
import com.hn.gege.sys.modules.sys.dto.UserRoleDTO;
import com.hn.gege.sys.modules.sys.entity.UserRole;
import com.hn.gege.sys.modules.sys.mapper.UserRoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;

/**
 * <p>
 * 用户角色关联 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2020-08-07
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Override
    public UserRole getByPk(String id) {
        LambdaQueryWrapper<UserRole> lambda = new QueryWrapper<UserRole>().lambda()
                .eq(UserRole::getId, id);
        return baseMapper.selectOne(lambda);
    }

    @Override
    public boolean updateByPk(UserRole userRole) {
        LambdaQueryWrapper<UserRole> lambda = new QueryWrapper<UserRole>().lambda()
                .eq(UserRole::getId, userRole.getId());
        return super.update(userRole, lambda);
    }

    @Override
    public boolean removeByPk(String id) {
        LambdaQueryWrapper<UserRole> lambda = new QueryWrapper<UserRole>().lambda()
                .eq(UserRole::getId, id);
        return super.remove(lambda);
    }

    @Transactional
    @Override
    public boolean setDefaultRole(String userId, String roleCode) {
        //先将所有用户角色设置为非默认
        LambdaUpdateWrapper<UserRole> updateWrapper = new UpdateWrapper<UserRole>().lambda()
                .set(UserRole::getSign, "0")
                .eq(UserRole::getUserId, userId);
        super.update(updateWrapper);


        //这里先删除在添加(为了方法通过--1.添加用户中设置角色,角色可能不存在;2.而用户管理中设置默认角色角色时存在的)
        super.remove(Wrappers.<UserRole>lambdaQuery()
                .eq(UserRole::getUserId, userId)
                .eq(UserRole::getRoleCode, roleCode));
        UserRole userRole = new UserRole().setUserId(userId).setRoleCode(roleCode).setSign("1");
        return  super.save(userRole);
    }

    @Override
    public Page<UserRoleDTO> userRolePage(Page page, String userId) {
        return baseMapper.userRolePage(page, userId);
    }

    @Override
    public Page<UserRoleDTO> unUserRolePage(Page page, String userId) {
        return baseMapper.unUserRolePage(page, userId);
    }

    @Override
    public Page<RoleUserDTO> roleUserPage(Page page, String roleCode) {
        return baseMapper.roleUserPage(page, roleCode);
    }

    @Override
    public Page<RoleUserDTO> unRoleUserPage(Page page, String roleCode) {
        return baseMapper.unRoleUserPage(page, roleCode);
    }

    @Override
    public boolean addUserRoles(String userId, String... rodeCodes) {
        LinkedList<UserRole> userRoles = new LinkedList<>();
        for (String str : rodeCodes) {
            UserRole userRole = new UserRole().setUserId(userId).setRoleCode(str);
            userRoles.add(userRole);
        }
        return super.saveBatch(userRoles);
    }

    @Override
    public boolean addRoleUsers(String rodeCode, String... userIds) {
        LinkedList<UserRole> userRoles = new LinkedList<>();
        for (String str : userIds) {
            UserRole userRole = new UserRole().setUserId(str).setRoleCode(rodeCode).setSign("1");
            userRoles.add(userRole);
        }
        return super.saveBatch(userRoles);
    }

    @Override
    public UserRoleDTO user(String userId, String sign) {
        return baseMapper.user(userId,sign);
    }

}
